package com.copyctrl.consumer.utils;

import com.copyctrl.campus.common.constant.MapperConstant;
import com.copyctrl.campus.common.domain.MysqlData;
import com.copyctrl.mapper.CampCommentMapper;
import com.copyctrl.mapper.CampPassageMapper;
import com.copyctrl.mapper.CampUserMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;

/**
 * @author:JH
 * @version:1.0
 */
@Component
public class MysqlUtil {
    @Resource
    CampCommentMapper campCommentMapper;
    @Resource
    CampUserMapper campUserMapper;
    @Resource
    CampPassageMapper campPassageMapper;

    public Object executeData(MysqlData mysqlData) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//        switch (mapperName){
//            case Comment:
//                mapper=campCommentMapper;
//                break;
//            case User:
//                mapper=campUserMapper;
//                break;
//            case Passage:
//                mapper=campPassageMapper;
//                break;
//            default:
//                throw new RuntimeException("没有对应的mapper");
//        }
        MapperConstant mapperName = mysqlData.getMapperConstantName();
        Integer invoke;
        if (mapperName.equals(MapperConstant.Comment)) {
            invoke = (Integer) campCommentMapper.getClass()
                    .getMethod(mysqlData.getMethod(), mysqlData.getData().getClass())
                    .invoke(campCommentMapper, mysqlData.getData());
        } else if (mapperName.equals(MapperConstant.User)) {
            invoke = (Integer) campUserMapper.getClass()
                    .getMethod(mysqlData.getMethod(), mysqlData.getData().getClass())
                    .invoke(campUserMapper, mysqlData.getData());
        } else if (mapperName.equals(MapperConstant.Passage)) {
            invoke = (Integer) campPassageMapper.getClass()
                    .getMethod(mysqlData.getMethod(), mysqlData.getData().getClass())
                    .invoke(campPassageMapper, mysqlData.getData());
        } else {
            throw new RuntimeException("没有对应的mapper");
        }

        return invoke;
    }

}
